## coffee -csb
# TODO: notification by websocket when other user click like button(point increase) or new geo point is added
google.maps.event.addDomListener( window, 'load', ->
  GakeIine =
    mapOptions :
      navigationControl: true
      mapTypeControl: true
      scaleControl: true
      center: new google.maps.LatLng(37.525792, 137.325482)
      zoom: 5
      mapTypeId: google.maps.MapTypeId.ROADMAP

    dataUrl: 'http://localhost:8000/data.json'

    audio : (->
      if HTMLAudioElement
        audio = new Audio("snd/josei1.wav")
        audio.volume = 1
        audio
    )()

    iine: ->
      audio = GakeIine.audio
      if audio?
        audio.load()
        audio.play()
      false

    buildEntry: (map, entry) ->
      if entry? and entry.lat? and entry.lng? and entry.img? and entry.id?
        {id, lat, lng, img} = entry
        latlng = new google.maps.LatLng(lat, lng)
        infocontent = $('<div></div>').attr('id', 'gake_' + id)
                      .append( $('<img></img>').attr('src',''))
                      .append( $('<span></span>').attr('id', 'iine_' + id).bind('click', GakeIine.iine)
                          .append($('<img></img>').attr({'class':'like', 'src':'img/like2.png'})))

        infowindow = new google.maps.InfoWindow({
          content: infocontent[0]
        })
        marker = new google.maps.Marker({
          position: latlng
          map: map
          title: 'Gake Iine!'
        })
        google.maps.event.addListener(marker, 'click', ->
          infowindow.open(map, marker)
        )

  $('#like').bind('click', ->
      audio = GakeIine.audio
      if audio?
        audio.load()
        audio.play()
      false
  )

  initialize = ->
    canvas = $('#map_canvas')
    map = new google.maps.Map(canvas[0], GakeIine.mapOptions) if canvas.length

    $.getJSON(GakeIine.dataUrl, (json)->
        for entry in json['entry']
          GakeIine.buildEntry(map, entry)
    )

  initialize()
)

# vim: set ft=coffee :
